package com.shop.loan.service.impl;

import com.shop.loan.mapper.EruptUserByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.EruptUserByMybatis;
import com.shop.loan.model.entity.NewMsgType;
import com.shop.loan.model.enums.ServiceType;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.MsgService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MsgServiceImpl implements MsgService {

    @Resource
    EruptUserService eruptUserService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    EruptUserByMybatisMapper eruptUserByMybatisMapper;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Override
    public ResResult hasNew() {
        //是否有新订单
        Long currentUid = eruptUserService.getCurrentUid();
        if (currentUid != null) {
            String key = RedisCache.sysAdminMsg + currentUid;
            String value = redisUtil.get(key);
            if (StringUtils.hasLength(value)) {
                redisUtil.del(key);
                return new ResResult(Code.SUCCESS);
            }
        }
        return new ResResult(Code.FAIL);
    }

    @Override
    public ResResult newMsgByAdmin(Long eruptUserId) {
        String key = RedisCache.sysAdminMsg + eruptUserId;
        redisUtil.set(key, "1");
        return null;
    }

    @Override
    public ResResult newMsgByOrder(NewMsgType newMsgType) {
        List<Long> eruptUserIds = new ArrayList<>();
        if (newMsgType.getType().intValue() == NewMsgType.loan.getType()) {
            //放款管理员列表
            List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(ServiceType.loan_service.getType());
            if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
                return null;
            }
            for (EruptUserByMybatis eruptUserByMybatis : eruptUserByMybatisList) {
                eruptUserIds.add(eruptUserByMybatis.getId());
            }
        } else if (newMsgType.getType().intValue() == NewMsgType.after.getType()) {
            //售后管理员列表
            List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(ServiceType.after_service.getType());
            if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
                return null;
            }
            for (EruptUserByMybatis eruptUserByMybatis : eruptUserByMybatisList) {
                eruptUserIds.add(eruptUserByMybatis.getId());
            }
        } else if (newMsgType.getType().intValue() == NewMsgType.collection.getType()) {
            //催收管理员列表
            List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(ServiceType.collection_service.getType());
            if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
                return null;
            }
            for (EruptUserByMybatis eruptUserByMybatis : eruptUserByMybatisList) {
                eruptUserIds.add(eruptUserByMybatis.getId());
            }
        }
        if (eruptUserIds.size() > 0) {
            for (Long eruptUserId : eruptUserIds) {
                String key = RedisCache.sysAdminMsg + eruptUserId;
                redisUtil.set(key, "1");
            }

        }
        return null;
    }


//    @Override
//    public ResResult newMsgBy(Long orderId, NewMsgType newMsgType) {
//        LoanOrderByMybatis loanOrderByMybatis = loanOrderByMybatisMapper.selectByPrimaryKey(orderId);
//
//        List<Long> eruptUserIds = new ArrayList<>();
//        if (newMsgType.getType().intValue() == NewMsgType.create.getType()) {
//            //新订单 给对应的审核人员
//            loanOrderByMybatis.getAuthServiceId();
//            if (loanOrderByMybatis.getAuthServiceId() != null) {
//                eruptUserIds.add(loanOrderByMybatis.getAuthServiceId());
//            }
//        } else if (newMsgType.getType().intValue() == NewMsgType.auth_pass.getType()) {
//            //给放款管理员
//            if (loanOrderByMybatis != null && loanOrderByMybatis.getLoanServiceId() != null) {
//                Long id = loanOrderByMybatis.getLoanServiceId();
//                eruptUserIds.add(id);
//            } else {
//                //放款管理员列表
//                List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(ServiceType.loan_service.getType());
//                if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
//                    return null;
//                }
//                for (EruptUserByMybatis eruptUserByMybatis : eruptUserByMybatisList) {
//                    eruptUserIds.add(eruptUserByMybatis.getId());
//                }
//            }
//        }
//        if (eruptUserIds != null && eruptUserIds.size() > 0) {
//            for (Long eruptUserId : eruptUserIds) {
//                String key = RedisCache.sysAdminMsg + eruptUserId;
//                redisUtil.set(key, "1");
//            }
//
//        }
//        return null;
//    }
}
